<?php
namespace app\shop\model;
use app\BaseModel;
use app\shop\model\IndexShopOrderGoods;
use think\facade\Db;

class IndexShopOrder extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_order';


    protected $append = [
        'goods_text',
        'status_text',
        'collage_text',
        'over_time_text',
        'staff_text',
        'store_name'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-06-24 13:29
     * @功能说明:门店名字
     */
    public function getStoreNameAttr($value,$data){

        if($data['store_id']){

            $store_model = new IndexStore();

            $store_name  = $store_model->where(['id'=>$data['store_id']])->value('title');

            return $store_name;
        }

    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 查询器 获取子订单
     */
    public function getGoodsTextAttr($value,$data)
    {
        $orer_goods     = new IndexShopOrderGoods();
        $dis['order_id']= $data['id'];
        $arr            = array();
        $arr['goods']   = $orer_goods->orderGoodsInfo($dis);
        $arr['count']   = $orer_goods->getGoodsNum($dis);
        return $arr;
    }

    /**
     * @param $value
     * @param $data
     * @return mixed
     * 查询器 订单状态
     */
    public function getStatusTextAttr($value,$data)
    {
        if ( $data[ 'pay_status' ] == 0 ) {
            $text = 1;//代付款
            if ( $data[ 'order_status' ] == 1 ) {
                $text = -1;//已取消
            }
        } elseif($data[ 'pay_status' ] == 1) {
            if ($data['order_status'] == 0) {
                $text = 2;//未发货
            }elseif ($data['order_status'] == 1) {
                $text = 7;//超时/取消
            }elseif ($data['order_status'] == 2) {
                $text = 3;//已发货
            }elseif ($data['order_status'] == 3) {
                $text = 4;//已完成
            }elseif($data['order_status'] == 4) {
                $text = 5;//已评价
            }
        }else{
            $text = 6;//退款
        }
        return !empty($text)?$text:'';
    }

    /**
     * @param $value
     * @param $data
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 拼团状态
     */
    public function getCollageTextAttr($value,$data){
        if ($data['type'] == 1){
            $collageInfo = Db::name('longbing_card_shop_collage_list')->where(['id' => $data['collage_id']])->value('collage_status');
            $status = $data['pay_status'] != 0?[1=>'拼团中',2=>'拼团成功',3=>'拼团完成',4=>'拼团失败']:[1=>'拼团中',2=>'拼团已结束',3=>'拼团已结束',4=>'超时未支付'];
            if(in_array($collageInfo,[1,2,3,4])){
                return $status[$collageInfo];
            }
        }
        return '';
    }
    /**
     * @param $value
     * @param $data
     * @return int|mixed
     * 倒计时
     */
    public function getOverTimeTextAttr($value,$data){

        $time  = Db::name('longbing_card_config')->where(['uniacid'=>$data['uniacid']])->value('order_overtime');

        return !empty($time)?$time:1800;
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 查询器 员工信息
     */
    public function getStaffTextAttr($value,$data){
        $staff_info = Db::name('longbing_card_user_info')->where(['fans_id'=>$data['to_uid']])->field('name,fans_id,id,avatar')->find();
        $staff_info = transImages($staff_info,['avatar']);
        return $staff_info;
    }
    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取订单列表
     */
    public function orderList($dis=array(),$page=10){
        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();
        return $data;
    }
    /**
     * @param $dis
     * @return mixed
     * 获取订单详情
     */
    public function orderInfo($dis,$field='*'){
        $data = self::where($dis)->field($field)->find();
        $data = !empty($data)?$data->toArray():$data;
        $data = !empty($data)?datachange($data):$data;
        return $data;
    }
    /**
     * @param $data
     * 添加订单
     */
    public function orderAdd($data){
        $res = $this->insert($data);
        $res = $this->getLastInsID();
        return $res;
    }

    /**
     * @param $dis
     * @param $data
     * @return IndexShopOrder
     * 修改订单
     */
    public function orderUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;
    }

    /**
     * @param $dis
     * 获取条数
     */
    public function orderCount($dis){
        $data = $this->where($dis)->count();
        return $data;
    }
    /**
     *
     * 订单状态
     */

    public function orderStatus($type){
        switch ( $type ) {
            //  1=>待付款
            case 1:
                $where[] = [ 'pay_status','=',0 ];
                $where[] = [ 'order_status','=',0 ];
                break;
            //  2=>待发货
            case 2:
                $where[] = [ 'pay_status','=',1 ];
                $where[] = [ 'order_status','=',0 ];
                break;
            //  3=>待收货
            case 3:

                $where[] = [ 'pay_status','=',1 ];
                $where[] = [ 'order_status','=',2 ];
                break;
            //  4=>已完成
            case 4:
                $where[] = [ 'pay_status','=',1 ];
                $where[] = [ 'order_status','>',2 ];
                break;
            //  5=>已取消
            case -1:
                $where[] = [ 'order_status','=',1 ];
                break;
            //  0=>全部订单
            default:
        }
        return $where;

    }


    /**
     * @param $type
     * @return mixed
     *
     * 退款订单状态
     */
    public function orderRefundStatus($type){
        switch ( $type ) {
            //  1=>退款审核中
            case 1:
                $where[] = [ 'pay_status','=',1];
                $where[] = [ 'refund_status','=',1];
                break;
            //  2=>已退款
            case 2:
                $where[] = [ 'pay_status','=',2];
                $where[] = [ 'refund_status','=',2];
                break;
            //  退款失败=>已退款
            case 3:
                $where[] = [ 'pay_status','=',3];
                $where[] = [ 'refund_status','=',3];
                break;
            //  0=>全部订单
            default:
                $where[] = [ 'pay_status','>',0];
                $where[] = [ 'refund_status','>',0];
        }
        return $where;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-31 17:21
     * @功能说明:检查订单
     */
    public function checkOrder(){



    }

}